const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu('component/TipsShell(悬浮提示)')
export default class TipsShell extends cc.Component {

    @property(cc.Label)
    Text: cc.Label = null;

    private _pool: cc.NodePool = null;

    public init(pool: cc.NodePool) {
        this._pool = pool;
    }

    public showTips(text: string) {
        if (!text || '' === text) {
            return;
        }
        this.node.opacity = 0;

        this.Text.overflow = cc.Label.Overflow.NONE;
        this.Text.string = text;
        this.Text['_forceUpdateRenderData'](true);
        let labelWidth = this.Text.node.width;
        if (labelWidth > cc.view.getFrameSize().width - 100) {
            this.Text.overflow = cc.Label.Overflow.RESIZE_HEIGHT;
            this.Text.node.width = cc.view.getFrameSize().width - 100;
        }
        this.Text['_forceUpdateRenderData'](true);

        this._begin();
    }

    private _begin() {
        let fadeIn = cc.fadeIn(0.2);
        let delay = cc.delayTime(2);
        let fadeOut = cc.fadeOut(1);
        let func = cc.callFunc(() => {
            this.Text.string = '';
            this._pool.put(this.node);
        })

        let sequ = cc.sequence(fadeIn, delay, fadeOut, func);

        this.node.runAction(sequ);
    }

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        
    }

    start() {

    }

    // update (dt) {}
}
